package by.grsu.na.page.sample;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;

import by.grsu.na.app.MyWebApplication;
import by.grsu.na.dao.impl.ClientDao;
import by.grsu.na.entity.Client;


public class ClientEditPage extends WebPage {

	public ClientEditPage(final Client client) {
		super();


		final Form<Client> form = new Form<Client>("form", new CompoundPropertyModel<Client>(client));
		add(form);


		form.add(new TextField<String>("clientName"));
		form.add(new TextField<String>("clientLogin"));
		form.add(new TextField<String>("clientPasword"));
		form.add(new TextField<String>("clientBlackEdit"));



		form.add(new SubmitLink("save") {
			@Override
			public void onSubmit() {
				super.onSubmit();

				final ClientDao clientDao = new ClientDao(MyWebApplication.DATA_XML_FOLDER);
				
				if (client.getId() == null) {

					clientDao.saveNew(client);
				} else {

					clientDao.update(client);
				}

				setResponsePage(new ClientsListPage());
			}
		});

		
		form.add(new Link("cancel") {
			@Override
			public void onClick() {

				setResponsePage(new ClientsListPage());
			}
		});
	}
}
